// 从本地获取信息并渲染
const userId = localStorage.getItem('user-id')
const token = localStorage.getItem('user-token')

//如果能获取到用户id，则渲染首页面中购物车商品的数量
if(userId && token) {
  axios({
    url:'/cart/list',
    headers:{
      'Access-Token':token,
      platform:'H5'
    }
  }).then(result=>{
    console.log(result)
    // console.log(result.data.data.cartTotal)
    if(result.data.data.cartTotal===0) {
      document.querySelector('.car_number').style.display = 'none'
    } else {
      document.querySelector('.car_number').innerHTML= result.data.data.cartTotal
      document.querySelector('.car_number').style.display = 'block'
    }
  })
} else {
  document.querySelector('.car_number').style.display = 'none'
}

// 获取用户id显示信息并渲染
if(userId && token) {
  document.querySelector('.center_back').style.display = 'block'
  axios.get('/user/info',{
    headers:{
      'Access-Token':token,
      platform:'H5'
    }
  }).then(result=>{
    // console.log(result)
    const userData = result.data.data.userInfo
    // console.log(userData)
    document.querySelector('.center_header a').innerHTML=`
      <div class="center_user"></div>
      <div class="center_login">
        <div class="center_login_top">${userData.nick_name}</div>
        <div class="center_login_bottom">${userData.mobile}</div>
      </div>
    `
  })

  // 获取数据渲染账户
  axios.get('/user/assets',{
    headers:{
      'Access-Token':token,
      platform:'H5'
    }
  }).then(result=>{
    console.log(result)
    console.log(result.data.data.assets)
    document.querySelector('#count').innerHTML=result.data.data.assets.balance
    document.querySelector('#points').innerHTML=result.data.data.assets.points
    document.querySelector('#coupon').innerHTML=result.data.data.assets.coupon
  })

  // 点击退出登录
  document.querySelector('.center_back').addEventListener('click',()=>{
    localStorage.removeItem('user-id')
    localStorage.removeItem('user-token')
    location.reload(true)
  })
}

